import {createRouter, createWebHistory} from 'vue-router'

// 配置路由
const routes = [
    {
        name: 'Index',
        path: '/',
        meta: {title: '首页'},
        component: () => import('@/views/portal/Index.vue')
    },
    {
        path: '/admin/login',
        name: 'admin-login',
        meta: {title: '控制台登录'},
        component: () => import('@/views/admin/Login.vue')
    },
    {
        path: '/admin',
        name: 'admin',
        redirect: '/admin/dashboard',
        component: () => import('@/views/admin/Home.vue'),
        meta: {title: 'Geek-AI 控制台'},
        children: [
            {
                path: '/admin/dashboard',
                name: 'admin-dashboard',
                component: () => import('@/views/admin/Dashboard.vue'),
                meta: {title: '仪表盘'},
            },
            {
                path: '/admin/user',
                name: 'admin-user',
                component: () => import('@/views/admin/User.vue'),
                meta: {title: '用户管理'},
            },
            {
                path: '/admin/app/type',
                name: 'admin-app-type',
                component: () => import('@/views/admin/AppType.vue'),
                meta: {title: '应用分类'},
            },
            {
                path: '/admin/app',
                name: 'admin-app',
                component: () => import('@/views/admin/Apps.vue'),
                meta: {title: '应用列表'},
            },
            {
                path: '/admin/apikey',
                name: 'admin-apikey',
                component: () => import('@/views/admin/ApiKey.vue'),
                meta: {title: 'API-KEY管理'},
            },
            {
                path: '/admin/chat/model',
                name: 'admin-chat-model',
                component: () => import('@/views/admin/ChatModel.vue'),
                meta: {title: '语言模型'},
            },
            {
                path: '/admin/product',
                name: 'admin-product',
                component: () => import('@/views/admin/Product.vue'),
                meta: {title: '充值产品'},
            },
            {
                path: '/admin/order',
                name: 'admin-order',
                component: () => import('@/views/admin/Order.vue'),
                meta: {title: '充值订单'},
            },
            {
                path: '/admin/redeem',
                name: 'admin-redeem',
                component: () => import('@/views/admin/Redeem.vue'),
                meta: {title: '兑换码管理'},
            },
            {
                path: '/admin/functions',
                name: 'admin-functions',
                component: () => import('@/views/admin/Functions.vue'),
                meta: {title: '函数管理'},
            },
            {
                path: '/admin/chats',
                name: 'admin-chats',
                component: () => import('@/views/admin/ChatList.vue'),
                meta: {title: '对话管理'},
            },
            {
                path: '/admin/images',
                name: 'admin-images',
                component: () => import('@/views/admin/ImageList.vue'),
                meta: {title: '绘画管理'},
            },
            {
                path: '/admin/medias',
                name: 'admin-medias',
                component: () => import('@/views/admin/Medias.vue'),
                meta: {title: '音视频管理'},
            },
            {
                path: '/admin/manger',
                name: 'admin-manger',
                component: () => import('@/views/admin/Manager.vue'),
                meta: {title: '管理员'},
            },
            {
                path: '/admin/system',
                name: 'admin-system',
                component: () => import('@/views/admin/SysConfig.vue'),
                meta: {title: '系统设置'},
            },
            {
                path: '/admin/powerLog',
                name: 'admin-powerLog',
                component: () => import('@/views/admin/PowerLog.vue'),
                meta: {title: '算力日志'},
            },
            {
                path: '/admin/loginLog',
                name: 'admin-loginLog',
                component: () => import('@/views/admin/LoginLog.vue'),
                meta: {title: '登录日志'},
            }
        ]
    },
    {
        name: 'Index',
        path: '/',
        meta: {title: '首页'},
        component: () => import('@/views/portal/Index.vue')
    },
    {
        path: '/login',
        name: 'login',
        meta: {title: '用户登录'},
        component: () => import('@/views/portal/Login.vue')
    },
    {
        path: '/register',
        name: 'register',
        meta: {title: '用户注册'},
        component: () => import('@/views/portal/Register.vue')
    },
    {
        path: '/home',
        name: 'home',
        //redirect: '/chat',
        component: () => import('@/views/portal/Home.vue'),
    },
]

// 创建路由
const router = createRouter({
    // 路由模式
    history: createWebHistory(),
    routes: routes
})

// 上一个路由对象
let prevRoute = null

// 全局前置守卫，在每次路由跳转前 触发逻辑
// to: 即将进入的路由对象（含 path、params、meta 等属性）
// from: 当前正要离开的路由对象
// next(): 必须调用的函数以继续导航流程
router.beforeEach((to,from,next) => {
    // 设置标题
    document.title = to.meta.title
    // 设置正要离开的路由对象
    prevRoute = from
    next()
})

export {router,prevRoute}